﻿using System;
using System.Collections.Generic;
using System.Linq;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Entity.Apply;
using studentSubsidy.Models;

namespace studentSubsidy.Service
{
    public class ApplyService
    {
        private ApplyDao _applyDao = new ApplyDao();
        private UserDao _userDao = new UserDao();
        private UploadService _uploadService = new UploadService();
        private AdminFinancialDao _adminFinancialDao = new AdminFinancialDao();

        public Result submitApply(ApplyQuery applyQuery)
        {
            _applyDao.insertApply(applyQuery);
            return new Result(200, "已完成申请，等待审核", null, null);
        }

        public Result isHasApply(int project_id, int user_id)
        {
            List<Apply> apply = _applyDao.isHasApply(project_id, user_id);
            if (apply.Count != 0)
            {
                if (apply[0].isPayment == 1)
                {
                    Project project = _applyDao.getProjectById(apply[0].project_id);
                    if (project.isRefund == 1)
                    {
                        Refund refund = _adminFinancialDao.getRefundByApplyId(apply[0].apply_id);
                        if (refund.state == 1)
                        {
                            return new Result(200, "", null, null);
                        }
                        else
                        {
                            return new Result(400, "所选项目已完成申请，请勿重复申请", null, null);
                        }
                    }
                    else
                    {
                        return new Result(200, "", null, null);
                    }
                }
                else
                {
                    return new Result(400, "所选项目已完成申请，请勿重复申请", null, null);
                }
            }
            else
            {
                return new Result(200, "", null, null);
            }
        }

        public Result getApply(int user_id)
        {
            List<Apply> projectList = _applyDao.getApplyProject(user_id);

            List<Project> projectInfo = new List<Project>();

            for (int i = 0; i < projectList.Count; i++)
            {
                Project project = _applyDao.getProjectById(projectList[i].project_id);
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }

        public Result getApplyDetail(ApplyDetailQuery applyDetailQuery)
        {
            User user = _applyDao.getUser(applyDetailQuery.userId);
            Project project = _applyDao.getProjectById(applyDetailQuery.projectId);
            Apply apply = _applyDao.getApplyById(applyDetailQuery.userId, applyDetailQuery.projectId);

            var applyDetailReturn = new ApplyDetailReturn
            {
                projectName = project.product_name,
                name = user.name,
                phone = user.phone,
                address = user.address,
                major = user.major,
                checkProgress = apply.check_progress,
                projectPrice = project.product_price,
                category = project.category_id,
                applyTime = apply.apply_time.Replace("T", " "),
                bankNumber = apply.bank_number,
                failRemark = apply.fail_remark
            };
            return new Result(200, "搜索成功", applyDetailReturn, null);
        }

        public Result getApplyDetailById(int applyId)
        {
            Apply apply = _applyDao.getApplyByApplyId(applyId);
            User user = _applyDao.getUser(apply.user_id);
            Project project = _applyDao.getProjectById(apply.project_id);

            var applyDetailReturn = new ApplyDetailReturn
            {
                projectName = project.product_name,
                name = user.name,
                phone = user.phone,
                address = user.address,
                major = user.major,
                checkProgress = apply.check_progress,
                projectPrice = project.product_price,
                category = project.category_id,
                applyTime = apply.apply_time.Replace("T", " "),
                bankNumber = apply.bank_number,
                failRemark = apply.fail_remark
            };
            return new Result(200, "搜索成功", applyDetailReturn, null);
        }

        public Result getApplyFile(ApplyDetailQuery applyDetailQuery)
        {
            List<Apply_File> applyFiles = _applyDao.getApplyFile(applyDetailQuery);
            return new Result(200, "搜索成功", applyFiles, null);
        }

        public Result getApplyFileById(int applyId)
        {
            Apply apply = _applyDao.getApplyByApplyId(applyId);
            ApplyDetailQuery applyDetailQuery = new ApplyDetailQuery
            {
                projectId = apply.project_id,
                userId = apply.user_id
            };
            List<Apply_File> applyFiles = _applyDao.getApplyFile(applyDetailQuery);
            return new Result(200, "搜索成功", applyFiles, null);
        }

        public Result DeleteApply(ApplyDetailQuery applyDetailQuery)
        {
            List<Apply_File> _applyFiles = _applyDao.getApplyFile(applyDetailQuery);

            for (int i = 0; i < _applyFiles.Count; i++)
            {
                int flag = _uploadService.UpLoadDelete(_applyFiles[i].file_name);
            }

            _applyDao.deleteApplyFile(applyDetailQuery);
            _applyDao.deleteApply(applyDetailQuery);
            return new Result(200, "搜索成功", null, null);
        }

        public Result getAllApply(string role)
        {
            List<Apply> applies = _applyDao.getApplyWithoutCheck(role);
            List<AllApplyReturn> allApplyReturns = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                User user = _applyDao.getUser(applies[i].user_id);
                var applyReturn = new AllApplyReturn
                {
                    applyId = applies[i].apply_id,
                    applyTime = applies[i].apply_time.Replace("T", " "),
                    projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                    address = user.address,
                    name = user.name,
                    major = user.major,
                    phone = user.phone
                };
                allApplyReturns.Add(applyReturn);
            }

            return new Result(200, "搜索成功", allApplyReturns, null);
        }


        public Result hasCheckList(int user_id)
        {
            List<Apply> projectList = _applyDao.hasCheckApply(user_id);

            List<Project> projectInfo = new List<Project>();

            for (int i = 0; i < projectList.Count; i++)
            {
                Project project = _applyDao.getProjectById(projectList[i].project_id);
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }

        public Result getRejectList(int user_id)
        {
            List<Apply> projectList = _applyDao.getRejectList(user_id);

            List<Project> projectInfo = new List<Project>();

            for (int i = 0; i < projectList.Count; i++)
            {
                Project project = _applyDao.getProjectById(projectList[i].project_id);
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }

        public Result getNeedSignedList(int userId)
        {
            List<Apply> applies = _applyDao.getNeedSignedList(userId);
            List<Project> projectInfo = new List<Project>();
            foreach (var VARIABLE in applies)
            {
                Project project = _applyDao.getProjectById(VARIABLE.project_id);
                project.category_id = VARIABLE.apply_id;
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }

        public Result getHasSignedCheckList(int userId)
        {
            List<Apply> applies = _applyDao.getHasSignedCheckList(userId);
            List<Project> projectInfo = new List<Project>();
            foreach (var VARIABLE in applies)
            {
                Project project = _applyDao.getProjectById(VARIABLE.project_id);
                project.category_id = VARIABLE.apply_id;
                projectInfo.Add(project);
            }

            return new Result(200, "搜索成功", projectInfo, null);
        }
    }
}